var app = getApp();
Page(Object.assign({}, {
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'admin',
            pdata : {
                op : 'redbaglog',
                page : 1,
                type : 0,
                cardid:0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        tab : {
            color : '#449aff',
            list: [{
            id: '0',
                title: '设置红包',
            }, {
            id: '1',
                title: '发放记录',
            }],
            selectedId: '0',
            scroll: false
        },
        card : {},
        actindex : 0,
        isredbag : false,
        isaddm : false,
        issetfee : false,
        startfee : '',
        endfee : '',
        startfeep : '',
        endfeep : '',
        addfee : '',
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        var cid = options.cid ? options.cid : 0;

        self.data.options = options;
        self.setData({
            datalist : [],
            'page.isend' : false,
            'page.pdata.page' : 1,
            'page.pdata.cardid' : cid,
        })

        app.com.auth(function(user){
            app.com.http('admin','POST',{op:'addcardstart',cid:cid},0,false,'','',function(res){
                
                if (!res.data.errno) {
                    var data = res.data.data;
                    self.setData({
                        card : data.card,
                        inited : true,
                        isredbag : data.card.isredbag == 1 ? true : false,
                        startfee : data.card.startfee,
                        startfeep : data.card.startfeep,
                        endfee : data.card.endfee,
                        endfeep : data.card.endfeep,
                        params : res.data.data.params,
                    })
                }else{
                    app.com.toast( res.data.message );
                    if( res.data.errno == 2 ){
                        app.globalData.adminurl = app.com.getUrlArgs();
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                }
            });
            self.onReachBottom();
        });
        wx.setNavigationBarTitle({
          title: '卡券红包',
        });
        
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: '卡券红包',
            path: '',
        }
    },
    onPullDownRefresh: function () {
        this.onLoad();
        // 关闭下拉刷新
        wx.stopPullDownRefresh();
    },
    tab(e) {
        this.setData({
            actindex : e.detail.index
        })
    },
    tolog(){
        this.setData({
            actindex :1
        })
    },
    addm(){
        this.setData({
            isaddm :!this.data.isaddm
        })
    },
    setfee(){
        this.setData({
            issetfee :!this.data.issetfee
        })
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,0,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    switchfn(e){
        var self = this;
        var type =  e.detail ? 1 : 0;
        var postdata = {op:'kgredbag',type :type,id:self.data.card.id};
        self.setData({
            isredbag : e.detail,
        })
        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno && res.data) {
                app.com.toast( res.data.message );
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        });

    },
    setfeeconfirm(){
        var self = this;
        var postdata = {
            op:'setredbagfee',
            startfee :self.data.startfee,
            endfee :self.data.endfee,
            startfeep :self.data.startfeep,
            endfeep :self.data.endfeep,
            needjf :self.data.needjf,
            id:self.data.card.id
        };

        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            if (res.data && !res.data.errno) {
                app.com.toast( res.data.message );
                self.setfee();
                self.onLoad(self.data.options);
            }else{
                app.com.toast( res.data.message );
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        },true);
    },
    bufa(e){
        var self = this;
        var id = e.currentTarget.dataset.id;
        var i = e.currentTarget.dataset.i;
        var postdata = {
            op:'bufaredbag',
            id:id
        };

        app.com.http('admin','POST',postdata,0,true,'','',function(res){
            app.com.toast( res.data.message );
            if (res.data && !res.data.errno) {

                self.onLoad(self.data.options);

            }else if( res.data.errno == 5 ){
                self.setData({
                    ['datalist['+i+'].status'] : 0,
                    ['datalist['+i+'].statusstr'] : '待发放',
                })
            }else{
                
                if( res.data.errno == 2 ){
                    wx.navigateTo({
                        url : res.data.data.url
                    });
                }
            }
        },true);
    },
    addconfirm(){
        var self = this;
        var postdata = {
            op : 'moneytoredbag',
            id : self.data.card.id,
            fee : self.data.addfee
        };
        app.com.confirm('确定转换吗？',function(){
            app.com.http('admin','POST',postdata,0,true,'','',function(res){

                if (res.data && !res.data.errno) {
                    app.com.toast( res.data.message );
                    self.onLoad(self.data.options);
                }else{
                    app.com.toast( res.data.message );
                    if( res.data.errno == 2 ){
                        wx.navigateTo({
                            url : res.data.data.url
                        });
                    }
                }
            });
        })
    },
    input(e){
        var key = e.currentTarget.dataset.key;
        this.setData({
            [`${key}`] : e.detail,
        })
    },
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    }
}))